package com.capgemini.fs.presentation.user.web.action;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.RoleService;
import com.capgemini.fs.app.ekms.service.UserService;
import com.capgemini.fs.presentation.user.web.form.ModifyUserForm;

public class ModifyUserAction extends Action {

	private UserService modifyUserService = null;
	private RoleService modifyRoleService = null;
	private GroupService modifyGroupService = null;

	public RoleService getModifyRoleService() {
		return modifyRoleService;
	}

	public void setModifyRoleService(RoleService modifyRoleService) {
		this.modifyRoleService = modifyRoleService;
	}

	public GroupService getModifyGroupService() {
		return modifyGroupService;
	}

	public void setModifyGroupService(GroupService modifyGroupService) {
		this.modifyGroupService = modifyGroupService;
	}

	public UserService getModifyUserService() {
		return modifyUserService;
	}

	public void setModifyUserService(UserService modifyUserService) {
		this.modifyUserService = modifyUserService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		ModifyUserForm modifyUserForm = (ModifyUserForm) form;

		HttpSession session = request.getSession();

		User candidate = modifyUserService.getUserById((Integer) session
				.getAttribute("userForwardId"));

		candidate.setUserName(modifyUserForm.getUsername());
		if (modifyUserForm.getPassword().equalsIgnoreCase("")) {

		} else {
			candidate.setSecurityPassword(modifyUserForm.getPassword());
			
		}

		candidate.setFirstName(modifyUserForm.getFirstname());
		candidate.setLastName(modifyUserForm.getLastname());

		candidate.setEmail(modifyUserForm.getEmail());
        
		//update user status
		if(modifyUserForm.getStatus()==0){
			
			candidate.setStatus(Constants.USER_STATUS_ACTIVE);
		}else{
			candidate.setStatus(Constants.USER_STATUS_INACTIVE);
		}
		//add join time
		if(candidate.getMemberType()==0)
			if(modifyUserForm.getCategory()=="employee"||modifyUserForm.getCategory().equalsIgnoreCase("employee")){
			candidate.setEntryDate(new Date());
			
		}
		
        //update category data
		
		if(modifyUserForm.getCategory()=="candidate" || modifyUserForm.getCategory().equalsIgnoreCase("candidate"))
		{	
			candidate.setMemberType(Constants.USER_MEMBERTYPE_TRAINEE);
		}else{
			candidate.setMemberType(Constants.USER_MEMBERTYPE_EMPLOYEE);
			
		}
		
		
		// update phone number
		candidate.setPhoneNo(modifyUserForm.getPhoneno());
		//update role
		if(modifyUserForm.getRole()==null||modifyUserForm.getRole().equals("")){
		}else{
			Set<Role> roles = new HashSet<Role>();
			String[] role = modifyUserForm.getRole();
			for (int i = 0; i < role.length; i++) {
				Role role1 = modifyRoleService.getRoleByName(role[i]);
				
				roles.add(role1);
			}
			candidate.setRoles(roles);
		}
		//update group
		
             if(modifyUserForm.getGroup()==null){
            	 candidate.setGroups(null);
             }else{
            	 Set<Group> groups = new HashSet<Group>();
            	 String[] group = modifyUserForm.getGroup();
            	 for(int i=0; i<group.length;i++){	
            		 Group group1=modifyGroupService.findGroupByGroupName(group[i]);
            		 groups.add(group1);
				
            	 }
            	 candidate.setGroups(groups);
             }
		//update last modifier' user name
		String modifier = (String) session.getAttribute("username");
		candidate.setLastModifier(modifier);
		System.out.println(modifier);
		
		try {			
			modifyUserService.updateUser(candidate);
			request.setAttribute("Success", "Saved Successfully!");
			return mapping.findForward("suc");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Failed", "Saved Failed!");
			return mapping.findForward("fail");
		}

	}

}
